﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BaHuan.BusinessLayer;
using BaHuan.Entities;
using BaHuan.Common;

namespace BaHuan
{
    public partial class FrmBCDTK : Form
    {
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private KhachHangManagement khachhangMgt = new KhachHangManagement();
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();
        private NganHangManagement nganhangMgt = new NganHangManagement();
        private NhanVienMgt nhanvienMgt = new NhanVienMgt();
        private PhieuThuTienManagement phieuthutienMgt = new PhieuThuTienManagement();
        private PhieuChiTienManagement phieuchitienMgt = new PhieuChiTienManagement();
        private CTTKKeToanLuongTinhManagement cttkketoanluongtinhMgt = new CTTKKeToanLuongTinhManagement();

        private string[] arrTKTaiSan = { "1", "2", "6", "8" };
        private string[] arrTKNguonVon = { "3", "4", "5", "7", "9" };
        private string[] arrTKNguonVonLuongTinh = { "331", "336" };
        private string[] arrTKDacBiet = { "214", "129", "139", "159", "229" };        
        private string[] arrTKTaiSanLuongTinh = { "111", "112", "131", "136", "141", "155", "156" };

        public FrmBCDTK()
        {
            InitializeComponent();
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            List<BangCanDoiTaiKhoan> lstTK = new List<BangCanDoiTaiKhoan>();
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            foreach (TaiKhoanKeToan a in GetAllTaiKhoanKeToan())
            {
                BangCanDoiTaiKhoan b = new BangCanDoiTaiKhoan();
                b.TenTK = a.TenTK;
                b.MaSoTK = a.MaSoTK;
                b.TKCha = a.TKCha;

                long lDuNoDauKy = 0;
                long lDuCoDauKy = 0;
                long lPSNo = 0;
                long lPSCo = 0;
                long lDuNoCuoiKy = 0;
                long lDuCoCuoiKy = 0;

                long lSoDuBanDau = a.SoDuBanDau;
                string sSub3Char = a.MaSoTK.Substring(0, 3); // First 3 char of MaSoTK
                string sSub1Char = a.MaSoTK.Substring(0, 1); // First char of MaSoTK

                // Danh sach cac doi tuong co so du ban dau
                List<CTTKKeToanLuongTinhForBCDTK> lstDoiTuong = GetAllCTTKKeToanLuongTinhForBCDTK(a.MaSoTK);

                // Get list of ButToans from the begining to dtDenNgay
                var lstAll = (from r in GetAllButToan()
                              where r.NgayThang.Date <= dtDenNgay && r.TKGoc == a.MaSoTK
                              select r);

                // Get list of ButToans from the begining to dtTuNgay
                var lstFirst = lstAll.Where(r => r.NgayThang.Date < dtTuNgay);

                // Get list of ButToans between dtTuNgay and dtDenNgay
                var lstBtw = lstAll.Where(r => r.NgayThang.Date >= dtTuNgay);

                if (arrTKDacBiet.Contains(sSub3Char))
                {
                    #region TK Dac Biet
                    long lTemp = lSoDuBanDau;
                    if (lstFirst.ToList().Count > 0)
                    {
                        foreach (var p in lstFirst)
                        {
                            lTemp = (lTemp + p.GhiCo) - p.GhiNo;
                        }

                        if (lTemp >= 0)
                            lDuCoDauKy = lTemp;
                        else
                            lDuNoDauKy = 0 - lTemp;
                    }

                    if (lstBtw.ToList().Count > 0)
                    {
                        foreach (var p in lstBtw)
                        {
                            lPSNo += p.GhiNo;
                            lPSCo += p.GhiCo;
                        }
                    }

                    if (lPSNo == 0 && lPSCo == 0)
                    {
                        lDuNoCuoiKy = lDuNoDauKy;
                        lDuCoCuoiKy = lDuCoDauKy;
                    }
                    else
                    {
                        long temp = (lTemp + lPSCo) - lPSNo;
                        if (temp >= 0)
                            lDuCoCuoiKy = temp;
                        else
                            lDuNoCuoiKy = 0 - temp;
                    }
                    #endregion
                }
                else if (arrTKTaiSan.Contains(sSub1Char))
                {
                    #region TK Tai San & Chi Phi
                    if (arrTKTaiSanLuongTinh.Contains(sSub3Char))
                    {
                        #region TK Luong Tinh
                        Dictionary<string, long> dictionary = new Dictionary<string, long>();
                        List<ButToanWithUNC> lstBTWUNC = GetAllButToanForBCDTK(dtDenNgay, a.MaSoTK);

                        // Add cac doi tuong vao dictionary
                        foreach (var o in lstDoiTuong)
                        {
                            if (o.TenChiNhanh != "")
                                dictionary.Add(o.TenChiNhanh, o.SoDuBanDau);
                            else if (o.TenNganHang != "")
                                dictionary.Add(o.TenNganHang, o.SoDuBanDau);
                            else if (o.TenKhachHang != "")
                                dictionary.Add(o.TenKhachHang, o.SoDuBanDau);
                            else if (o.HoTen != "")
                                dictionary.Add(o.HoTen, o.SoDuBanDau);
                        }

                        List<ButToanWithUNC> l = lstBTWUNC.Where(p => p.NgayThang.Date < dtTuNgay.Date).ToList();

                        // Duyet qua danh sach cac but toan voi dieu kien < TuNgay
                        if (l.Count > 0)
                        {
                            foreach (var p in l)
                            {
                                // Neu doi tuong chua co trong dictionary thi add vao dictionary
                                if (a.MaSoTK.StartsWith("112"))
                                {
                                    if (!dictionary.ContainsKey(p.TenNganHang))
                                        dictionary.Add(p.TenNganHang, p.GhiNo - p.GhiCo);
                                    else // Doi tuong da co trong dictionary thi lay value de tiep tuc tinh toan
                                        dictionary[p.TenNganHang] = (dictionary[p.TenNganHang] + p.GhiNo) - p.GhiCo;
                                }
                                else
                                {
                                    if (!dictionary.ContainsKey(p.MaSoKhachHang))
                                        dictionary.Add(p.MaSoKhachHang, p.GhiNo - p.GhiCo);
                                    else // Doi tuong da co trong dictionary thi lay value de tiep tuc tinh toan
                                        dictionary[p.MaSoKhachHang] = (dictionary[p.MaSoKhachHang] + p.GhiNo) - p.GhiCo;
                                }
                            }

                            foreach (var pair in dictionary)
                            {
                                if (pair.Value >= 0)
                                    lDuNoDauKy += pair.Value;
                                else if (pair.Value < 0)
                                    lDuCoDauKy += 0 - pair.Value;
                            }
                        }

                        List<ButToanWithUNC> lBtw = lstBTWUNC.Where(p => p.NgayThang.Date >= dtTuNgay.Date).ToList();

                        // Truong hop co danh sach so du dau ky, dung tinh toan voi cac so phat sinh trong list TuNgay - DenNgay
                        if (dictionary.Count > 0 && lBtw.Count > 0)
                        {
                            foreach (var r in lBtw)
                            {
                                lPSNo += r.GhiNo;
                                lPSCo += r.GhiCo;

                                long TempSoDu = 0;
                                if (a.MaSoTK.StartsWith("112"))
                                {
                                    if (!dictionary.ContainsKey(r.TenNganHang))
                                    {
                                        TempSoDu = r.GhiNo - r.GhiCo;
                                        dictionary.Add(r.TenNganHang, TempSoDu);
                                    }
                                    else
                                    {
                                        TempSoDu = (dictionary[r.TenNganHang] + r.GhiNo) - r.GhiCo;
                                        dictionary[r.TenNganHang] = TempSoDu;
                                    }
                                }
                                else
                                {
                                    if (!dictionary.ContainsKey(r.MaSoKhachHang))
                                    {
                                        TempSoDu = r.GhiNo - r.GhiCo;
                                        dictionary.Add(r.MaSoKhachHang, TempSoDu);
                                    }
                                    else
                                    {
                                        TempSoDu = (dictionary[r.MaSoKhachHang] + r.GhiNo) - r.GhiCo;
                                        dictionary[r.MaSoKhachHang] = TempSoDu;
                                    }
                                }
                            }

                            foreach (var pairs in dictionary)
                            {
                                if (pairs.Value >= 0)
                                    lDuNoCuoiKy += pairs.Value;
                                else if (pairs.Value < 0)
                                    lDuCoCuoiKy += 0 - pairs.Value;
                            }
                        }
                        else if (dictionary.Count > 0 && lBtw.Count == 0) // Truong hop danh sach cac so phat sinh TuNgay - DenNgay khong co
                        {
                            lDuNoCuoiKy = lDuNoDauKy;
                            lDuCoCuoiKy = lDuCoDauKy;
                        }
                        else if (dictionary.Count == 0 && lBtw.Count > 0) // Truong hop khong cac danh sach so du dau ky
                        {
                            foreach (var r in lBtw)
                            {
                                lPSNo += r.GhiNo;
                                lPSCo += r.GhiCo;

                                long TempSoDu = 0;
                                TempSoDu = r.GhiNo - r.GhiCo;

                                if (TempSoDu >= 0)
                                    lDuNoCuoiKy += TempSoDu;
                                else if (TempSoDu < 0)
                                    lDuCoCuoiKy += 0 - TempSoDu;
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        #region TK Thuong
                        long lTemp = lSoDuBanDau;
                        if (lstFirst.ToList().Count > 0)
                        {
                            foreach (var p in lstFirst)
                            {
                                lTemp = (lTemp + p.GhiNo) - p.GhiCo;
                            }

                            if (lTemp >= 0)
                                lDuNoDauKy = lTemp;
                            else
                                lDuCoDauKy = 0 - lTemp;
                        }

                        if (lstBtw.ToList().Count > 0)
                        {
                            foreach (var p in lstBtw)
                            {
                                lPSNo += p.GhiNo;
                                lPSCo += p.GhiCo;
                            }
                        }

                        if (lPSNo == 0 && lPSCo == 0)
                        {
                            lDuNoCuoiKy = lDuNoDauKy;
                            lDuCoCuoiKy = lDuCoDauKy;
                        }
                        else
                        {
                            long temp = (lTemp + lPSNo) - lPSCo;
                            if (temp >= 0)
                                lDuNoCuoiKy = temp;
                            else
                                lDuCoCuoiKy = 0 - temp;
                        }
                        #endregion
                    }
                    #endregion
                }
                else if (arrTKNguonVon.Contains(sSub1Char))
                {
                    #region TK Nguon Von & Doanh Thu
                    if (arrTKNguonVonLuongTinh.Contains(sSub3Char))
                    {
                        #region TK Luong Tinh
                        Dictionary<string, long> dictionary = new Dictionary<string, long>();

                        // Add cac doi tuong vao dictionary
                        foreach (var l in lstDoiTuong)
                        {
                            if (l.TenChiNhanh != "")
                                dictionary.Add(l.TenChiNhanh, l.SoDuBanDau);
                            else
                                dictionary.Add(l.TenNhaCungCap, l.SoDuBanDau);
                        }

                        // Duyet qua danh sach cac but toan voi dieu kien < TuNgay
                        if (lstFirst.ToList().Count > 0)
                        {
                            foreach (var p in lstFirst)
                            {
                                // Neu doi tuong chua co trong dictionary thi add vao dictionary
                                if (!dictionary.ContainsKey(p.MaSoKhachHang))
                                    dictionary.Add(p.MaSoKhachHang, p.GhiCo - p.GhiNo);
                                else // Doi tuong da co trong dictionary thi lay value de tiep tuc tinh toan
                                    dictionary[p.MaSoKhachHang] = (dictionary[p.MaSoKhachHang] + p.GhiCo) - p.GhiNo;
                            }

                            // Xet gia tri cho cac bien DuNoDauKy va DuCoDauKy
                            foreach (var pair in dictionary)
                            {
                                if (pair.Value >= 0)
                                    lDuCoDauKy += pair.Value;
                                else if (pair.Value < 0)
                                    lDuNoDauKy += 0 - pair.Value;
                            }
                        }

                        // Truong hop co danh sach so du dau ky, dung tinh toan voi cac so phat sinh trong list TuNgay - DenNgay
                        if (dictionary.Count > 0 && lstBtw.ToList().Count > 0)
                        {
                            foreach (var l in lstBtw)
                            {
                                lPSNo += l.GhiNo;
                                lPSCo += l.GhiCo;
                                
                                long TempSoDu = 0;
                                if (!dictionary.ContainsKey(l.MaSoKhachHang))
                                {
                                    TempSoDu = l.GhiCo - l.GhiNo;
                                    dictionary.Add(l.MaSoKhachHang, TempSoDu);
                                }
                                else
                                {
                                    TempSoDu = (dictionary[l.MaSoKhachHang] + l.GhiCo) - l.GhiNo;
                                    dictionary[l.MaSoKhachHang] = TempSoDu;
                                }
                            }

                            foreach (var pairs in dictionary)
                            {
                                if (pairs.Value >= 0)
                                    lDuCoCuoiKy += pairs.Value;
                                else if (pairs.Value < 0)
                                    lDuNoCuoiKy += 0 - pairs.Value;
                            }
                        }
                        else if (dictionary.Count > 0 && lstBtw.ToList().Count == 0) // Truong hop danh sach cac so phat sinh TuNgay - DenNgay khong co
                        {
                            lDuNoCuoiKy = lDuNoDauKy;
                            lDuCoCuoiKy = lDuCoDauKy;
                        }
                        else if (dictionary.Count == 0 && lstBtw.ToList().Count > 0) // Truong hop khong cac danh sach so du dau ky
                        {
                            foreach (var l in lstBtw)
                            {
                                lPSNo += l.GhiNo;
                                lPSCo += l.GhiCo;

                                long TempSoDu = 0;
                                TempSoDu = l.GhiCo - l.GhiNo;

                                if (TempSoDu >= 0)
                                    lDuCoCuoiKy += TempSoDu;
                                else if (TempSoDu < 0)
                                    lDuNoCuoiKy += 0 - TempSoDu;
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        #region TK Thuong
                        long lTemp = lSoDuBanDau;
                        if (lstFirst.ToList().Count > 0)
                        {
                            foreach (var p in lstFirst)
                            {
                                lTemp = (lTemp + p.GhiCo) - p.GhiNo;
                            }

                            if (lTemp >= 0)
                                lDuCoDauKy = lTemp;
                            else
                                lDuNoDauKy = 0 - lTemp;
                        }

                        if (lstBtw.ToList().Count > 0)
                        {
                            foreach (var p in lstBtw)
                            {
                                lPSNo += p.GhiNo;
                                lPSCo += p.GhiCo;
                            }
                        }

                        if (lPSNo == 0 && lPSCo == 0)
                        {
                            lDuNoCuoiKy = lDuNoDauKy;
                            lDuCoCuoiKy = lDuCoDauKy;
                        }
                        else
                        {
                            long temp = (lTemp + lPSCo) - lPSNo;
                            if (temp >= 0)
                                lDuCoCuoiKy = temp;
                            else
                                lDuNoCuoiKy = 0 - temp;
                        }
                        #endregion
                    }
                    #endregion
                }

                b.DuNoDauKy = lDuNoDauKy;
                b.DuCoDauKy = lDuCoDauKy;
                b.PhatSinhNo = lPSNo;
                b.PhatSinhCo = lPSCo;
                b.DuNoCuoiKy = lDuNoCuoiKy;
                b.DuCoCuoiKy = lDuCoCuoiKy;

                lstTK.Add(b);
            }

            if (lstTK.Count > 0)
            {
                #region Sum ve tai khoan cap 1
                List<BangCanDoiTaiKhoan> lstParent = (from p in lstTK
                                                      where p.TKCha == "000"
                                                      select p).ToList();

                foreach (BangCanDoiTaiKhoan t in lstParent)
                {
                    List<BangCanDoiTaiKhoan> lstChildren = (from p in lstTK
                                                            where p.TKCha == t.MaSoTK
                                                            select p).ToList();

                    if (lstChildren.Count > 0)
                    {
                        foreach (var l in lstChildren)
                        {
                            t.DuNoDauKy += l.DuNoDauKy;
                            t.DuCoDauKy += l.DuCoDauKy;
                            t.DuNoCuoiKy += l.DuNoCuoiKy;
                            t.DuCoCuoiKy += l.DuCoCuoiKy;
                            t.PhatSinhNo += l.PhatSinhNo;
                            t.PhatSinhCo += l.PhatSinhCo;
                        }

                        int index = lstTK.FindIndex(p => p.MaSoTK == t.MaSoTK);
                        lstTK[index] = t;
                    }
                }
                #endregion

                #region Lay cac tai khoan co phat sinh
                var sub = (from l in lstTK
                           where l.DuNoDauKy == 0 && l.DuNoCuoiKy == 0
                                && l.DuCoDauKy == 0 && l.DuCoCuoiKy == 0
                                && l.PhatSinhCo == 0 && l.PhatSinhNo == 0
                           select l);

                lstTK = (from r in lstTK
                         where !sub.Contains(r)
                         select r).ToList();
                #endregion

                FrmXemReport frm = new FrmXemReport(lstTK, dtTuNgay, dtDenNgay);
                this.Hide();
                frm.ShowDialog();
                this.Show();
            }
            else
            {
                MessageBox.Show("Không có dữ liệu phù hợp!");
            }
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<KhachHang> GetAllKhachHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khachhangMgt.GetAllKhachHang();
            List<KhachHang> listKhachHang = (List<KhachHang>)resultObj.Result;

            return listKhachHang;
        }

        public List<NhaCungCap> GetAllNhaCungCap()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nhacungcapMgt.GetAllNhaCungCap();
            List<NhaCungCap> listNhaCungCap = (List<NhaCungCap>)resultObj.Result;

            return listNhaCungCap;
        }

        public List<ButToanWithUNC> GetAllButToanForBCDTK(DateTime dt, string MaSoTK)
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButtoanForBCDTK(dt, MaSoTK);
            List<ButToanWithUNC> listButToanWithUNC = (List<ButToanWithUNC>)resultObj.Result;

            return listButToanWithUNC;
        }

        public List<CTTKKeToanLuongTinhForBCDTK> GetAllCTTKKeToanLuongTinhForBCDTK(string MaSoTK)
        {
            ResultObj resultObj = new ResultObj();

            resultObj = cttkketoanluongtinhMgt.GetAllCTTKKeToanLuongTinhForBCDTK(MaSoTK);
            List<CTTKKeToanLuongTinhForBCDTK> listCTTKKeToanLuongTinhForBCDTK = (List<CTTKKeToanLuongTinhForBCDTK>)resultObj.Result;

            return listCTTKKeToanLuongTinhForBCDTK;
        }

        public List<NganHang> GetAllNganHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nganhangMgt.GetAllNganHang();
            List<NganHang> listNganHang = (List<NganHang>)resultObj.Result;

            return listNganHang;
        }

        public List<NhanVien> GetAllNhanVien()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nhanvienMgt.GetAllNhanVien();
            List<NhanVien> listNhanVien = (List<NhanVien>)resultObj.Result;

            return listNhanVien;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return listChiNhanhCongTy;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            List<TaiKhoanKeToan> lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }

        public List<PhieuThuTien> GetAllPhieuThuTien()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuthutienMgt.GetAllPhieuThuTien();
            List<PhieuThuTien> lstPhieuThuTien = (List<PhieuThuTien>)resultObj.Result;

            return lstPhieuThuTien;
        }

        public List<PhieuChiTien> GetAllPhieuChiTien()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuchitienMgt.GetAllPhieuChiTien();
            List<PhieuChiTien> lstPhieuChiTien = (List<PhieuChiTien>)resultObj.Result;

            return lstPhieuChiTien;
        }

        public List<CTTKKeToanLuongTinh> GetAllCTTKKeToanLuongTinh()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = cttkketoanluongtinhMgt.GetAllCTTKKeToanLuongTinh();
            List<CTTKKeToanLuongTinh> listCTTKKeToanLuongTinh = (List<CTTKKeToanLuongTinh>)resultObj.Result;

            return listCTTKKeToanLuongTinh;
        }
    }
}
